﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RentalSystem.Data.EF;
using System.Data.Objects;
using Web.Models;
using Web.Helper;

namespace Web.Controllers
{
    public class PublicController : BaseController
    {
        [HttpGet]
        public ActionResult Start()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Locations(int page = 1)
        {
            int pageSize = 5;
            List<Agencies> agencies = DataContext.AgenciesDao.List();

            foreach (Agencies a in agencies)
            {
                if (a.AgencyName.Equals("Default"))
                {
                    agencies.Remove(a);
                    break;
                }
            }

            LocationListViewModel model = new LocationListViewModel
            {
                List = agencies.OrderBy(p => p.AgencyName).Skip((page - 1) * pageSize).Take(pageSize),
                PagingInfo = new PagingInfo(page,pageSize,agencies.Count)
            };

            ViewBag.IsUserLogged = CurrentUser != null && !CurrentUser.IsWorker;
            LoadErrorsAndSuccessFromAnotherPage();
            SetFormMode(Mode.Display);
            return View(model);
        }

        [HttpGet]
        public ActionResult Discounts()
        {
            return View(GetPageContent("DiscountsPage"));
        }

        [HttpGet]
        public ActionResult Regulations()
        {
            return View(GetPageContent("RulesPage"));
        }

        [HttpGet]
        public ActionResult Contact()
        {
            return View(GetPageContent("ContactPage"));
        }

        private PageContentData GetPageContent(String key)
        {
            PagesContents content = DataContext.PagesContentsDao.GetContentByTitle(key);
            PageContentData model = new PageContentData();

            if (content != null)
                model = new PageContentData { Id = content.PagesContentID, Content = content.PagesContentContent };

            return model;
        }
    }
}
